(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.ppfCommunity.listController', listController);
	listController.$inject = [ '$scope', '$q', 'Constants', 'gridService', 'modalService', 'layoutConfig', 'validatorConfig', 'authService', 
	                           'ppfCommunityService', 'ajaxServiceFactory', 'commonService', 'profFilterOptionService' ];
	function listController($scope, $q, Constants, gridService, modalService, layoutConfig, validatorConfig, authService, 
			ppfCommunityService, ajaxServiceFactory, commonService, profFilterOptionService) {
		var ctrl = this, info = $scope.$stateParams.params, log = $scope.loggerFactory.getLogger("ppfCommunityList"), ajaxService = ajaxServiceFactory.buildAjaxService({
		});
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		
		ctrl.init = function() {
			ctrl.setupDataStatusOptions = commonService.setupDataStatusOptions();
			ctrl.gridOptions = gridService.createGridOptions($scope, ctrl.layoutConfig.tableConfigs.table, paginationChanged, serviceRowSelectionChanged, sortChanged);
			ctrl.gridOptions.data = 'vm.searchCriteria.list';
			ctrl.searchCriteria = ctrl.gridOptions.defaultSearchCriteria;
			ppfCommunityService.initOptions(ctrl)//1. search option
			.then(function() {// 2. filter options
				return $q(function(resolved, rejected) {
					profFilterOptionService.initCommunityListFilterOptions(ctrl).then(function() {
						resolved();
					});
				});
			})
			.then(function() {// 3. get list
				ctrl.search();
			});
		};
		
		ctrl.view = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			$scope.go('cda.ppfCommunity.view', ctrl.selectedRows[0]);
		};

		ctrl.create = function() {
			var params = {};
			$scope.go('cda.ppfCommunity.create', params);
		};

		ctrl.update = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			$scope.go('cda.ppfCommunity.update', ctrl.selectedRows[0]);
		};

		ctrl.delete = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			$scope.go('cda.ppfCommunity.delete', ctrl.selectedRows[0]);
		};

		ctrl.search = function() {
			ctrl.searchCriteria.list = [];
			authService.getList(ctrl.searchCriteria).then(
			function(response) {
				angular.merge(ctrl.searchCriteria, response.data);
				gridService.updateGridOptions(ctrl.searchCriteria, ctrl.gridOptions);
			},angular.noop);
		};

		function paginationChanged(pageNum, pageSize) {
			ctrl.searchCriteria.pageNum = pageNum;
			ctrl.searchCriteria.pageSize = pageSize;
			ctrl.searchCriteria.list = [];
			ctrl.search();
		}

		function serviceRowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
		}

		function sortChanged(sortColumns) {
			// now just support sort by one column
			ctrl.searchCriteria.sortDirection = sortColumns[0] ? sortColumns[0].direction : '';
			ctrl.searchCriteria.sortBy = sortColumns[0] ? sortColumns[0].sortBy : '';
			ctrl.search();
		}
	}
})(angular);